#include <iostream>
#include <cstdio>
#include <csignal>
#include <unistd.h>
#include <sys/types.h>

void PrintPending(sigset_t *set)
{
	printf("当前进程是：%d, pending:", getpid());
	for (int i = 31; i > 0; i--)
	{
		if (sigismember(set, i))
		{
			std::cout << 1;
		}
		else
		{
			std::cout << 0;
		}
	}
	std::cout << std::endl;
	sleep(1);
}

void sighandler(int signum){
	printf("接收到了%d号信号\n", signum);
}

int main()
{
	// 1、屏蔽2号信号
	sigset_t block, oldblock;
	sigemptyset(&block);
	sigemptyset(&oldblock);
	sigaddset(&block, SIGINT);
	sigprocmask(SIG_SETMASK, &block, &oldblock);

	int cnt = 0;
	while (true)
	{
		// 获取pending集合
		sigset_t pending;
		int n = sigpending(&pending);
		if (n != 0)
		{
			std::cout << "get pending error" << std::endl;
		}

		// 封装打印函数
		PrintPending(&pending);

		if (cnt == 10)
		{
			// 还原
			signal(SIGINT, sighandler);
			sigprocmask(SIG_SETMASK, &oldblock, nullptr);
		}
		cnt++;
	}

	return 0;
}